package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *@param shoppingCartDTO
     * */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        Long userId = BaseContext.getCurrentId();
        cart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        //判断是否表中存在该菜品
        if(list != null && !list.isEmpty()){
            //如果有，number加1
            ShoppingCart shoppingCart = list.get(0);
            shoppingCart.setId(list.get(0).getId());
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }
        //如果没有，插入
        else{
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            //判断添加购物车的是菜品还是套餐
            if(dishId != null){
                //查询dish表中的数据，并返回给dish对象
                Dish dish = dishMapper.getById(dishId);
                cart.setAmount(dish.getPrice());
                cart.setImage(dish.getImage());
                cart.setName(dish.getName());
            }
            else{
                //查询setmeal表中的数据，并返回给setmeal对象
                Setmeal setmeal = setmealMapper.getById(setmealId);
                cart.setAmount(setmeal.getPrice());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
            }
            cart.setCreateTime(LocalDateTime.now());
            cart.setNumber(1);
            shoppingCartMapper.insert(cart);

        }
    }

    /**
     * 查看购物车
     * @Return
     * */
    public List<ShoppingCart> list() {

        Long userId = BaseContext.getCurrentId();//获取当前操作人的id
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
         return shoppingCartMapper.list(cart);

    }

    /**
     * 清除购物车
     * */
    public void cleanShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.cleanShoppingCart(userId);

    }

    /**
     * 删除购物车中一个商品
     * */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart cart = new ShoppingCart();
        cart.setUserId(userId);
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        //查询该商品的数量，如果数量大于1，数量减1
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        Integer number = list.get(0).getNumber();
        cart.setId(list.get(0).getId());
        if(number > 1){
           number= number-1;
           cart.setNumber(number);
           shoppingCartMapper.updateNumberById(cart);
        }else{
            shoppingCartMapper.subShoppingCart(cart);
        }
    }
}
